package com.aidong.myprofile.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.myprofile.biz.MyDuationLablesBiz;
import com.aidong.myprofile.entity.MyDuationLables;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 我的个性标签
 * @author java
 * @version 1.0    
 *  2015.07.05
 */
@ParentPackage("json")
@Namespace("/")
public class MyDuationLablesAction implements ModelDriven<MyDuationLables> {
	
	private MyDuationLables duationLables;

	@Override
	public MyDuationLables getModel() {
		if(duationLables == null){
			duationLables = new MyDuationLables();
		}
		return duationLables;
	}
	
	private MyDuationLablesBiz duationLablesBiz; // 个性标签service
	
	private Map<String, Object> map;

	/**
	 * 个性标签service
	 * @param duationLablesBiz
	 */
	@Autowired
	public void setDuationLablesBiz(MyDuationLablesBiz duationLablesBiz) {
		this.duationLablesBiz = duationLablesBiz;
	}

	@JSON
	public Map<String, Object> getMap() {
		return map;
	}
	
	/**
	 * 获得我的个性标签
	 * @return
	 */
	@Action(value="getmyduationlables",results={@Result(name="succ",type="json",params={"root","map"})})
	public String getMyDuationLables(){
		map = new HashMap<String, Object>();
		try {
			int idongId = duationLables.getIdongId(); // 爱动号
			if(idongId > 0){
				List<Map<String, Object>> duationLables = duationLablesBiz.getMyDuationLables(idongId);
				map.put("code", 1);
				map.put("result", duationLables);
				return "succ";
			}else{
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
	
	/**
	 * 添加我的个性标签
	 * @return
	 */
	@Action(value="addmyduationlables",results={@Result(name="succ",type="json",params={"root","map"})})
	public String addMyDuationLables(){
		map = new HashMap<String, Object>();
		try {
			int idongId = duationLables.getIdongId(); // 爱动号
			int color = duationLables.getColor(); // 背景颜色
			String tagName = duationLables.getTagName(); // 标签名
			if(idongId > 0 && color > 0
					&& (!"".equals(tagName) && tagName != null)){
				Random rnd = new Random();
				int p = rnd.nextInt(10);
				duationLables.setSortId(p); // 排序
				duationLablesBiz.addMyDuationLables(duationLables);
				map.put("code", 1);
				map.put("result", "添加成功");
				return "succ";
			}else{
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
	
	/**
	 * 删除我的个性标签
	 * @return
	 */
	@Action(value="delmyduationlables",results={@Result(name="succ",type="json",params={"root","map"})})
	public String delMyDuationLables(){
		map = new HashMap<String, Object>();
		try {
			int idongId = duationLables.getIdongId(); // 爱动号
			int tagId = duationLables.getTagId(); // 标签ID
			if(idongId > 0 && tagId > 0){
				Map<String, Object> paraMap = new HashMap<String, Object>();
				paraMap.put("idongId", idongId);
				paraMap.put("tagId", tagId);
				duationLablesBiz.delMyDuationLables(paraMap);
				
				map.put("code", 1);
				map.put("result", "删除成功");
				return "succ";
			}else{
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}

}
